import Vue from 'vue'
import co from 'co'

let cancelUserGen = function * (data, type) {
  console.log('销户。。', data)
  if (type === '销户') {
    let owe = yield Vue.resetpost('rs/logic/hasOweWithUser', {data: {f_userinfo_id: data.record_userinfo}}, {resolveMsg: null, rejectMsg: null})
    console.log('销户看是否有欠费', owe.data)
    let hasHandplan = yield Vue.resetpost('rs/sql/singleTable', {data: {tablename: 't_handplan', condition: `f_meter_state = '未抄表' and f_userinfo_id = '${data.record_userinfo}'`}}, {resolveMsg: null, rejectMsg: null})
    console.log('是否有未抄表数据', hasHandplan.data)
    if (owe.data === 1 && hasHandplan.data.length > 0) {
      Vue.showAlert('该用户还有欠费，不能销户！！', 'danger', 0)
    } else {
      Vue.resetpost('rs/entity/t_cancellation', data, {resolveMsg: `${type}成功`, rejectMsg: `${type}失败`})
    }
  } else {
    Vue.resetpost('rs/entity/t_cancellation', data, {resolveMsg: `${type}成功`, rejectMsg: `${type}失败`})
  }
}

let FileService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.FileService = Vue.prototype.$FileService = FileService
  },

  fileSave (data) {
    console.log('档案保存，，，，，', data)
    let result = Object.assign({}, result, data.baseinfo.base)
    result.f_userinfo_id = result.f_userinfo_id ? result.f_userinfo_id + '' : null
    result.f_user_type = result.f_user_type.length > 0 ? result.f_user_type[0] : ''
    result.f_cost_type = result.f_cost_type.length > 0 ? result.f_cost_type[0] : ''
    result.f_gasproperties = result.f_gasproperties.length > 0 ? result.f_gasproperties[0] : ''
    result.f_credentials = result.f_credentials.length > 0 ? result.f_credentials[0] : ''
    result.f_paper_type = result.f_paper_type.length > 0 ? result.f_paper_type[0] : ''
    result.f_adjustable_id = result.f_adjustable.length > 0 ? result.f_adjustable[0].id : null
    result.f_predeposit = result.f_predeposit === '' ? 0 : result.f_predeposit
    if (result.f_area instanceof Array) {
      result.f_area = result.f_area.length > 0 ? result.f_area[0] : null
    }
    if (result.f_slice_area instanceof Array) {
      result.f_slice_area = result.f_slice_area.length > 0 ? result.f_slice_area[0] : null
    }
    if (data.baseinfo.base.f_cost_type[0] === '银行代扣') {
      let bank = Object.assign({}, bank, data.baseinfo.bank)
      result.infobank = bank
      result.infobank.f_bank_name = bank.f_bank_name.length > 0 ? bank.f_bank_name[0] : ''
    }
    if (result.f_userinfo_id === null) {
      result.f_operator = this.$login.f.name
      result.f_filiale = this.$login.f.f_fengongsi
      result.f_outlets = this.$login.f.f_parentname
    }
    result.f_user_state = result.f_user_state.length > 0 ? result.f_user_state[0] : '正常'
    result.userfiles = []
    data.meterinfo.forEach((item) => {
      // item.f_user_id = result.f_user_id ? result.f_user_id : null
      let meter = Object.assign({}, meter, item)
      meter.f_user_id = meter.f_user_id ? meter.f_user_id + '' : null
      meter.f_position = meter.f_position.length > 0 ? meter.f_position[0] : null
      meter.f_gasbrand_id = meter.gasbrand[0].id
      meter.f_inputtor = meter.f_inputtor.length > 0 ? meter.f_inputtor[0] : null
      meter.f_install_person = meter.f_install_person.length > 0 ? meter.f_install_person[0] : null
      meter.f_aroundmeter = meter.f_aroundmeter.length > 0 ? meter.f_aroundmeter[0] : null
      meter.f_meter_base = isNaN(meter.f_meter_base) || meter.f_meter_base === '' ? 0 : meter.f_meter_base
      meter.f_remanent_gas = isNaN(meter.f_meter_base) || meter.f_remanent_gas === '' ? 0 : meter.f_remanent_gas
      meter.f_total_fee = 0
      // meter.f_install_person = meter.f_install_person.length > 0 ? meter.f_install_person[0] : ''
      // meter.f_gas_person = meter.f_gas_person.length > 0 ? meter.f_gas_person[0] : ''
      // meter.f_gas_person_2 = meter.f_gas_person_2.length > 0 ? meter.f_gas_person_2[0] : ''
      // meter.f_changetube_person = meter.f_changetube_person.length > 0 ? meter.f_changetube_person[0] : ''
      if (meter.f_fire_state1 === true) {
        meter.f_fire_state = '1'
      } else if (meter.f_fire_state1 === false) {
        meter.f_fire_state = '0'
      }
      meter.f_gasmodel_id = meter.gasmodel[0].id
      meter.f_user_type = result.f_user_type
      meter.f_gasproperties = result.f_gasproperties

      if (!meter.f_user_id) {
        meter.f_table_state = '正常'
        meter.f_initial_base = meter.f_meter_base
      }
      item.gasbrand[0].f_meter_type.includes('卡表') ? meter.f_whether_hairpin = '未发' : meter.f_whether_hairpin = '无卡'
      meter.f_meter_classify = meter.gasbrand[0].f_meter_type
      meter.f_price_id = meter.pricename[0].f_price_id
      if (meter.f_user_id === null) {
        meter.f_filiale = Util.f.f_fengongsi
        meter.f_outlets = Util.f.f_parentname
      }
      meter.img = meter.img ? meter.img : {content: null, filename: null}
      meter.img.content = meter.img.content ? meter.img.content : null
      meter.img.filename = meter.img.filename ? meter.img.filename : null
      result.userfiles.push(meter)
    })
    let devices = []
    data.devicesinfo.forEach((item) => {
      let device = Object.assign({}, device, item)
      device.f_devices_type = device.f_devices_type[0]
      device.f_devices_state = device.f_devices_state ? device.f_devices_state : '有效'
      device.img = device.img ? device.img : {content: null, filename: null}
      device.img.content = device.img.content ? device.img.content : null
      device.img.filename = device.img.filename ? device.img.filename : null
      devices.push(device)
    })
    result.f_change_reason = ''
    // 拼接原因（表具修改表底数和剩余气量时修改）
    if (data.baseinfo.base.f_userinfo_id) {  // 表示修改
      for (var i = 0; i < data.meterinfo.length; i++) {
        if (data.meterinfo[i].f_meter_base_reason) {
          result.f_change_reason += data.meterinfo[i].f_meter_base_reason
        }
        if (data.meterinfo[i].f_remanent_gas_reason) {
          result.f_change_reason += data.meterinfo[i].f_remanent_gas_reason
        }
      }
    }
    return Vue.resetpost('rs/logic/fileSave', {data: {userinfo: result, devices: devices}})
  },
  batchOperate (model, type) {
    let data = {
      f_operat_type: '销户',
      f_describe: `${Util.f.name}对用户${model.userinfo.f_user_name}进行销户`,
      f_state: '有效',
      f_user_id: model.userinfo.f_user_id,
      record_userinfo: model.userinfo.f_userinfo_id,
      f_user_name: model.userinfo.f_user_name,
      f_cancel_state: type,
      f_cancel_cause: model.f_reason,
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname
    }
    if (type === '销户') {
      data.userinfo = {
        f_userinfo_id: model.userinfo.f_userinfo_id,
        f_user_state: '销户',
        version: model.userinfo.version
      }
    } else if (type === '启用') {
      data.f_describe = `${Util.f.name}对用户${model.userinfo.f_user_name}进行启用`
      data.userinfo = {
        f_userinfo_id: model.userinfo.f_userinfo_id,
        f_user_state: '正常',
        version: model.userinfo.version
      }
    }
    let cancelUser = cancelUserGen(data, type)
    return co(cancelUser)
  }
}

export default FileService
